package com.gusti.admincasa.util;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

import javax.swing.JOptionPane;

/**
 * 
 * @author gusti
 *
 */
public class Debug {

	public static boolean debugActivated = true;

	/**
	 * 
	 * @param log
	 * 		message to log into console
	 */
	public static void log( String log ){
		if( debugActivated && log != null ){
			System.out.println( "[ " + getDateTime() + " ] INFO: " + log );
		}
	}

	/**
	 * 
	 * @param e
	 * 		exception to log into console
	 */
	public static void error( Exception e ){
		if( debugActivated && e != null ){
			System.out.println( "[ " + getDateTime() + " ] ERROR: " + e.getClass().toString() + " | " + e.getMessage() );
		}
	}

	private static String getDateTime() {
		GregorianCalendar gregorianCalendar = new GregorianCalendar();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

		return simpleDateFormat.format( gregorianCalendar.getTime() );
	}

	/**
	 * 
	 * @param message
	 * 		message to show in JOptionPane
	 */
	public static void showMessage( String message ) {
		if( debugActivated && message != null && !message.isEmpty() ) {
			new Debug().showMessageDialog( message );
		}
	}

	private void showMessageDialog( String message ) {
		new MessageDialogThread( message ).start();
	}

	private class MessageDialogThread extends Thread {

		private String message = null;
		private boolean isMessage = false;

		public MessageDialogThread( String message ) {
			super( "MessageDialogThread" );
			Debug.log( "Message Dialog thread started" );
			this.message = message;
			this.isMessage = true;
		}

		@Override
		public void run() {
			
			while( isMessage ) {
				JOptionPane.showMessageDialog( null, message, "Message", JOptionPane.INFORMATION_MESSAGE );
				isMessage = false;
			}
			
		}

	}
}
